<?php
// $Id$

/**
 * Controller_Admin_Web 控制器
 */
class Controller_Admin_fxlevelset extends Controller_Abstract {
	
	/**
	 * 分销等级设置
	 */
	function actionIndex()
	{
		$this->union_table = fxlevelset::meta();
		$append_data=array("acct_id"=>$_SESSION['acct']['id']);

		//获取搜索参数
		$this->get_param();
		//初始化分页信息
		$this->_format_page_info();
		
		//关联表
		//$this->union_table = $union_table;
		if (!is_null($append_data))$this->param = array_merge($this->param,$append_data);
		
		$dis_param = new helper_disposeparam($this->param);
		$sql = $dis_param->getsql();//OUT PUT用
		//dump($sql);die;
		$_SESSION['lastsql'] = $sql;//OUT PUT用
		$obj = call_user_func_array(array($this->union_table,'find'), $sql);
		$item = $obj->limitPage($this->curr_page,$this->page_size)
					->order(' grade asc')
					->fetchPagination($this->pagination)
					->getall();
		$this->_view['item'] = $item;
		$this->_view['pagination'] = $this->pagination;
		$this->_view['orderField'] = $this->orderkey;
		$this->_view['orderDirection'] = $this->ordertype;

		// $this->orderkey="";
		// $this->lists($append_data);
	}
	 
	/**
	 * 分销等级设置
	 */
	 
	function actionAdd() {
	    //设置要操作的表单，并设置自动插入方式
	    $this->union_table = fxlevelset::meta ();
	    //验证输入
	    if($this->_context->isPOST()){

	    	if(!$this->_context->id){
	    		$level = fxlevelset::find(
	    		array(
	    			"acct_id"=>$_SESSION['acct']['id'],
	    			"type"=>$this->_context->type,
	    			"grade"=>$this->_context->grade,
	    		))
	    		->getOne();
	    		if($level->id){
	    			$this->error("已经存在此条设置，不能重复添加");
	    		}
	    	}
	    }
	    $handle = new helper_autoinsert ( $this->union_table );
	    $form = new Form_fxlevelset_add ( url ( $this->_context->controller_name . '/' . $this->_context->action_name ) );
	     
 
	    //这里面会自动将get方式获取的参数传递到表单里面去的
	    $result = $handle->add ( $form, 'id', $_GET );
	    if ($this->_context->isGET ()) {
	        //传递$form值
	        $this->_view ['form'] = $result;
	        $this->_viewname = '../form/add';
	        $form->element('acct_id')->value = $_SESSION['acct']['id'];

	    } else {
	        //记录日志
	        if($_POST['id'])	{
	            Syslog::logs ( '添加分销等级设置,id :' . $handle->form_database_obj->id );
	        }else{
	            Syslog::logs ( '编辑分销等级设置,id :' . $handle->form_database_obj->id );
	        }
	        if($handle->status=='success')
	            $this->_context->forwardUrl = url("fxlevelset/index");
	        return call_user_func_array(array($this,$handle->status),array($result));
	    }
	     
	}
	 
	/**
	 * 删除会员等级
	 */
	function actionDel() {
	     Syslog::logs("删除分销等级设置，ID：".print_r($this->_context->ids,true));
	     $this->union_table = fxlevelset::meta();
	     $this->delete(array('id'=>$this->_context->ids));
	}


}


